<?php
/*
    This function is written by Cheng-Lin Yang and Polley Wong. Please be free to mail us
    if there is any problem. :)

    Contact information:
        Cheng-Lin Yang  <s0170154@csie.mcu.edu.tw>
        Polley Wong     <polley@gmail.com>
                                                                2005.4.1
    
    ChangeLog:
        1. add preview_tdspace variable for adjust preview image to best size.  2005.4.5.
*/

$lastPage = $gallery->album->numPhotos(1);
if($lastPage <6){
    $start = 1;
    $end= $lastPage;
} elseif($index == 1 || $index == 2){
    $start = 1;
    $end = 5;
} elseif($index == $lastPage || $index == $lastPage -1){
    $start = $lastPage - 4;
    $end = $lastPage;
} else{
    $start = $index - 2;
    $end = $index + 2;
}

//adj preview table look
switch($lastPage){
    case "1":
        $pre_space = 43;
        break;
    case "2":
        $pre_space = 35;
        break;
    case "3":
        $pre_space = 30;
        break;
    case "4":
        $pre_space = 25;
        break;
    default:
        $pre_space = 20;
}

$pre_pictures = array();
for($i=$start; $i<=$end; $i++){
    $pre_picture = array();
    $scaleTo = $gallery->album->fields["thumb_size"];
    $itemID = $gallery->album->getPhotoId($i);
    $pre_picture['index'] = $i;
    if ($gallery->album->isMovie($itemID)) {
            $pre_picture['image'] = $gallery->album->getThumbnailTag($i);
            $pre_picture['link'] = $gallery->album->getPhotoPath($i);
    } elseif ($gallery->album->isAlbumName($i)) {
            $myAlbumName = $gallery->album->isAlbumName($i);
            $myAlbum = new Album();
            $myAlbum->load($myAlbumName);

            $pre_picture['image'] = $myAlbum->getHighlightAsThumbnailTag($scaleTo);
            $pre_picture['link'] = makeAlbumUrl($myAlbumName);

    } else {
            $photo = $gallery->album->getPhoto($i);
            $pre_picture['image'] = $gallery->album->getThumbnailTag($i);
            $xoops_imageid = $photo->image->getXoopsImageId();
            $xoops_imageid = !empty($xoops_imageid) ? intval($xoops_imageid) : 0;
            $pre_picture['link'] = makeAlbumUrl($gallery->session->albumName, $itemID, array('xoops_imageid' => $xoops_imageid));
    }
    //resize the image
    $sizeTimes = 0.65;
    
    $oriWidth = substr($pre_picture['image'], strpos($pre_picture['image'], "width")+7, strpos($pre_picture['image'], "\" height")-strlen($pre_picture['image']));
    $oriHeight = substr($pre_picture['image'], strpos($pre_picture['image'], "height")+8, -strpos(strrev($pre_picture['image']), "redrob")-9);
    $pre_picture['image'] = str_replace('width="'.$oriWidth.'"', 'width="'.$oriWidth*$sizeTimes.'"', $pre_picture['image']);
    $pre_picture['image'] = str_replace('height="'.$oriHeight.'"', 'height="'.$oriHeight*$sizeTimes.'"', $pre_picture['image']);
    $pre_pictures[] =& $pre_picture;
    unset($pre_picture);
}
$xoopsTpl->assign('PreviewthumbImage', $pre_pictures);
$xoopsTpl->assign('preview_tdspace', $pre_space);
?>